package view;

import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import model.Suppliers;
import database.dao.SuppliersDAO;

public class FrmSuppliers extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private int currentRecord = 0;
	private int maxRecord = 0;

	private JPanel jContentPane = null;

	private JLabel jlb_supplierid = null;

	private JTextField jtxfld_supplierid = null;

	private JLabel jlb_companyname = null;

	private JLabel jlb_contactname = null;

	private JLabel jlb_title = null;

	private JTextField jtxfld_companyname = null;

	private JTextField jtxfld_conatcname = null;

	private JTextField jtxfld_title = null;

	private JTextField jtxfld_address = null;

	private JTextField jtxfld_fax = null;

	private JTextField jtxfld_city = null;

	private JTextField jtxfld_postalcode = null;

	private JLabel jlb_address = null;

	private JLabel jlb_city = null;

	private JLabel jlb_region = null;

	private JLabel jlb_postalcode = null;

	private JLabel jlb_country = null;

	private JTextField jtxfld_region = null;

	private JLabel jlb_phone = null;

	private JTextField jtxfld_phone = null;

	private JLabel jlb_fax = null;

	private JTextField jtxfld_country = null;

	private JLabel jlb_homepage = null;

	private JTextField jtxfld_homepage = null;

	private JPanel jpnl_campos = null;

	private JPanel jpnl_botoes = null;

	private JButton jbtn_first = null;

	private JButton jbtn_previous = null;

	private JTextField jtxfld_currentrecord = null;

	private JButton jbtn_next = null;

	private JButton jbtn_last = null;

	private JButton jbtn_new = null;

	private JButton jbtn_delete = null;

	private JButton jbtn_desfazer = null;

	/**
	 * This is the default constructor
	 */
	public FrmSuppliers() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(550, 350);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Suppliers");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.jtxfld_supplierid.setEnabled(false);
		this.setVisible(true);
		initialRecord();
	}
	
	private void initialRecord(){
		try {
			SuppliersDAO supDao = new SuppliersDAO();
			List<Suppliers> resultado = supDao.getListSuppliers();
			maxRecord = resultado.size();
			if (maxRecord >= 0) {
				jtxfld_supplierid.setText(resultado.get(currentRecord).getSupplierid().toString());
				jtxfld_companyname.setText(resultado.get(currentRecord).getCompanyname());
				jtxfld_conatcname.setText(resultado.get(currentRecord).getContactname());
				jtxfld_title.setText(resultado.get(currentRecord).getContacttitle());
				jtxfld_address.setText(resultado.get(currentRecord).getAddress());
				jtxfld_city.setText(resultado.get(currentRecord).getCity());
				jtxfld_region.setText(resultado.get(currentRecord).getRegion());
				jtxfld_postalcode.setText(resultado.get(currentRecord).getPostalcode());
				jtxfld_country.setText(resultado.get(currentRecord).getCountry());
				jtxfld_phone.setText(resultado.get(currentRecord).getPhone());
				jtxfld_fax.setText(resultado.get(currentRecord).getFax());
				jtxfld_homepage.setText(resultado.get(currentRecord).getHomepage());
				jtxfld_currentrecord.setText(Integer.toString(currentRecord+1));
			} else {
				JOptionPane.showMessageDialog(null, "No record in database");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jlb_homepage = new JLabel();
			jlb_homepage.setBounds(new Rectangle(10, 220, 100, 21));
			jlb_homepage.setText("Home Page:");
			jlb_fax = new JLabel();
			jlb_fax.setBounds(new Rectangle(340, 190, 50, 21));
			jlb_fax.setText("Fax:");
			jlb_phone = new JLabel();
			jlb_phone.setBounds(new Rectangle(10, 190, 100, 21));
			jlb_phone.setText("Phone:");
			jlb_country = new JLabel();
			jlb_country.setBounds(new Rectangle(340, 160, 50, 21));
			jlb_country.setText("Country:");
			jlb_postalcode = new JLabel();
			jlb_postalcode.setBounds(new Rectangle(10, 160, 100, 21));
			jlb_postalcode.setText("Postal Code:");
			jlb_region = new JLabel();
			jlb_region.setBounds(new Rectangle(340, 130, 50, 21));
			jlb_region.setText("Region:");
			jlb_city = new JLabel();
			jlb_city.setBounds(new Rectangle(10, 130, 100, 21));
			jlb_city.setText("City:");
			jlb_address = new JLabel();
			jlb_address.setBounds(new Rectangle(10, 100, 100, 21));
			jlb_address.setText("Address:");
			jlb_title = new JLabel();
			jlb_title.setBounds(new Rectangle(340, 70, 30, 21));
			jlb_title.setText("Title:");
			jlb_contactname = new JLabel();
			jlb_contactname.setBounds(new Rectangle(10, 70, 100, 21));
			jlb_contactname.setText("Contact Name:");
			jlb_companyname = new JLabel();
			jlb_companyname.setBounds(new Rectangle(10, 40, 100, 21));
			jlb_companyname.setText("Company Name:");
			jlb_supplierid = new JLabel();
			jlb_supplierid.setText("Supplier ID:");
			jlb_supplierid.setBounds(new Rectangle(10, 10, 100, 21));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJpnl_campos(), null);
			jContentPane.add(getJpnl_botoes(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jtxfld_supplierid	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_supplierid() {
		if (jtxfld_supplierid == null) {
			jtxfld_supplierid = new JTextField();
			jtxfld_supplierid.setBounds(new Rectangle(120, 10, 125, 21));
			jtxfld_supplierid.setToolTipText("Supplier ID");
		}
		return jtxfld_supplierid;
	}

	/**
	 * This method initializes jtxfld_companyname	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_companyname() {
		if (jtxfld_companyname == null) {
			jtxfld_companyname = new JTextField();
			jtxfld_companyname.setBounds(new Rectangle(120, 40, 400, 21));
			jtxfld_companyname.setToolTipText("Company Name");
		}
		return jtxfld_companyname;
	}

	/**
	 * This method initializes jtxfld_conatcname	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_conatcname() {
		if (jtxfld_conatcname == null) {
			jtxfld_conatcname = new JTextField();
			jtxfld_conatcname.setBounds(new Rectangle(120, 70, 215, 21));
			jtxfld_conatcname.setToolTipText("Contact Name");
		}
		return jtxfld_conatcname;
	}

	/**
	 * This method initializes jtxfld_title	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_title() {
		if (jtxfld_title == null) {
			jtxfld_title = new JTextField();
			jtxfld_title.setBounds(new Rectangle(375, 70, 145, 21));
			jtxfld_title.setToolTipText("Title");
		}
		return jtxfld_title;
	}

	/**
	 * This method initializes jtxfld_address	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_address() {
		if (jtxfld_address == null) {
			jtxfld_address = new JTextField();
			jtxfld_address.setBounds(new Rectangle(120, 100, 400, 21));
			jtxfld_address.setToolTipText("Address");
		}
		return jtxfld_address;
	}

	/**
	 * This method initializes jtxfld_fax	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_fax() {
		if (jtxfld_fax == null) {
			jtxfld_fax = new JTextField();
			jtxfld_fax.setBounds(new Rectangle(400, 190, 120, 21));
			jtxfld_fax.setToolTipText("Fax");
		}
		return jtxfld_fax;
	}

	/**
	 * This method initializes jtxfld_city	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_city() {
		if (jtxfld_city == null) {
			jtxfld_city = new JTextField();
			jtxfld_city.setBounds(new Rectangle(120, 130, 215, 21));
			jtxfld_city.setToolTipText("City");
		}
		return jtxfld_city;
	}

	/**
	 * This method initializes jtxfld_postalcode	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_postalcode() {
		if (jtxfld_postalcode == null) {
			jtxfld_postalcode = new JTextField();
			jtxfld_postalcode.setBounds(new Rectangle(120, 160, 215, 21));
			jtxfld_postalcode.setToolTipText("Postal Code");
		}
		return jtxfld_postalcode;
	}

	/**
	 * This method initializes jtxfld_region	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_region() {
		if (jtxfld_region == null) {
			jtxfld_region = new JTextField();
			jtxfld_region.setBounds(new Rectangle(400, 130, 120, 21));
			jtxfld_region.setToolTipText("Region");
		}
		return jtxfld_region;
	}

	/**
	 * This method initializes jtxfld_phone	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_phone() {
		if (jtxfld_phone == null) {
			jtxfld_phone = new JTextField();
			jtxfld_phone.setBounds(new Rectangle(120, 190, 215, 21));
			jtxfld_phone.setToolTipText("Phone");
		}
		return jtxfld_phone;
	}

	/**
	 * This method initializes jtxfld_country	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_country() {
		if (jtxfld_country == null) {
			jtxfld_country = new JTextField();
			jtxfld_country.setBounds(new Rectangle(400, 160, 120, 21));
			jtxfld_country.setToolTipText("Country");
		}
		return jtxfld_country;
	}

	/**
	 * This method initializes jtxfld_homepage	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_homepage() {
		if (jtxfld_homepage == null) {
			jtxfld_homepage = new JTextField();
			jtxfld_homepage.setBounds(new Rectangle(120, 220, 400, 21));
			jtxfld_homepage.setToolTipText("Hpme Page");
		}
		return jtxfld_homepage;
	}

	/**
	 * This method initializes jpnl_campos	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJpnl_campos() {
		if (jpnl_campos == null) {
			jpnl_campos = new JPanel();
			jpnl_campos.setLayout(null);
			jpnl_campos.setBounds(new Rectangle(5, 5, 530, 250));
			jpnl_campos.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
			jpnl_campos.add(jlb_supplierid, null);
			jpnl_campos.add(jlb_companyname, null);
			jpnl_campos.add(jlb_contactname, null);
			jpnl_campos.add(jlb_title, null);
			jpnl_campos.add(jlb_address, null);
			jpnl_campos.add(jlb_city, null);
			jpnl_campos.add(jlb_region, null);
			jpnl_campos.add(jlb_postalcode, null);
			jpnl_campos.add(jlb_country, null);
			jpnl_campos.add(jlb_phone, null);
			jpnl_campos.add(jlb_fax, null);
			jpnl_campos.add(jlb_homepage, null);
			jpnl_campos.add(getJtxfld_supplierid(), null);
			jpnl_campos.add(getJtxfld_companyname(), null);
			jpnl_campos.add(getJtxfld_conatcname(), null);
			jpnl_campos.add(getJtxfld_title(), null);
			jpnl_campos.add(getJtxfld_address(), null);
			jpnl_campos.add(getJtxfld_fax(), null);
			jpnl_campos.add(getJtxfld_city(), null);
			jpnl_campos.add(getJtxfld_postalcode(), null);
			jpnl_campos.add(getJtxfld_region(), null);
			jpnl_campos.add(getJtxfld_phone(), null);
			jpnl_campos.add(getJtxfld_country(), null);
			jpnl_campos.add(getJtxfld_homepage(), null);
		}
		return jpnl_campos;
	}

	/**
	 * This method initializes jpnl_botoes	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJpnl_botoes() {
		if (jpnl_botoes == null) {
			jpnl_botoes = new JPanel();
			jpnl_botoes.setLayout(null);
			jpnl_botoes.setBounds(new Rectangle(5, 260, 530, 50));
			jpnl_botoes.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
			jpnl_botoes.add(getJbtn_first(), null);
			jpnl_botoes.add(getJbtn_previous(), null);
			jpnl_botoes.add(getJtxfld_currentrecord(), null);
			jpnl_botoes.add(getJbtn_next(), null);
			jpnl_botoes.add(getJbtn_last(), null);
			jpnl_botoes.add(getJbtn_new(), null);
			jpnl_botoes.add(getJbtn_delete(), null);
			jpnl_botoes.add(getJbtn_desfazer(), null);
		}
		return jpnl_botoes;
	}

	/**
	 * This method initializes jbtn_first	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtn_first() {
		if (jbtn_first == null) {
			jbtn_first = new JButton();
			jbtn_first.setBounds(new Rectangle(50, 15, 50, 21));
			jbtn_first.setToolTipText("FIRST");
			jbtn_first.setIcon(new ImageIcon(getClass().getResource("/res/First.gif")));
			jbtn_first.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (currentRecord > 0){
						currentRecord = 0;
						initialRecord();
					}else {
						JOptionPane.showMessageDialog(null, "Primeiro registro");
					}
				}
			});
		}
		return jbtn_first;
	}

	/**
	 * This method initializes jbtn_previous	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtn_previous() {
		if (jbtn_previous == null) {
			jbtn_previous = new JButton();
			jbtn_previous.setBounds(new Rectangle(105, 15, 50, 21));
			jbtn_previous.setToolTipText("PREVIOUS");
			jbtn_previous.setIcon(new ImageIcon(getClass().getResource("/res/Prev.gif")));
			jbtn_previous.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (currentRecord > 0){
						currentRecord--;
						initialRecord();
					} else {
						JOptionPane.showMessageDialog(null, "Primeiro registro");
					}
				}
			});
		}
		return jbtn_previous;
	}

	/**
	 * This method initializes jtxfld_currentrecord	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxfld_currentrecord() {
		if (jtxfld_currentrecord == null) {
			jtxfld_currentrecord = new JTextField();
			jtxfld_currentrecord.setBounds(new Rectangle(160, 15, 50, 22));
			jtxfld_currentrecord.setToolTipText("CURRENT RECORD");
			jtxfld_currentrecord.addKeyListener(new java.awt.event.KeyAdapter() {   
				public void keyReleased(java.awt.event.KeyEvent e) {
					if ((e.getKeyCode() == KeyEvent.VK_ENTER))
						if ((Integer.parseInt(jtxfld_currentrecord.getText()) >= 1) &
							(Integer.parseInt(jtxfld_currentrecord.getText()) <= maxRecord)){
						currentRecord = Integer.parseInt(jtxfld_currentrecord.getText())-1;
						initialRecord();
					}
				}
			});
		}
		return jtxfld_currentrecord;
	}

	/**
	 * This method initializes jbtn_next	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtn_next() {
		if (jbtn_next == null) {
			jbtn_next = new JButton();
			jbtn_next.setBounds(new Rectangle(215, 15, 50, 21));
			jbtn_next.setToolTipText("NEXT");
			jbtn_next.setIcon(new ImageIcon(getClass().getResource("/res/Next.gif")));
			jbtn_next.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (currentRecord < maxRecord-1){
						currentRecord++;
						initialRecord();
					} else {
						JOptionPane.showMessageDialog(null, "Ultimo registro");
					}
				}
			});
		}
		return jbtn_next;
	}

	/**
	 * This method initializes jbtn_last	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtn_last() {
		if (jbtn_last == null) {
			jbtn_last = new JButton();
			jbtn_last.setBounds(new Rectangle(270, 15, 50, 21));
			jbtn_last.setToolTipText("LAST");
			jbtn_last.setIcon(new ImageIcon(getClass().getResource("/res/Last.gif")));
			jbtn_last.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (currentRecord < maxRecord-1){
						currentRecord = maxRecord-1;
						initialRecord();
					} else {
						JOptionPane.showMessageDialog(null, "Ultimo registro");
					}
				}
			});
		}
		return jbtn_last;
	}

	/**
	 * This method initializes jbtn_new	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtn_new() {
		if (jbtn_new == null) {
			jbtn_new = new JButton();
			jbtn_new.setBounds(new Rectangle(325, 15, 50, 21));
			jbtn_new.setToolTipText("NEW");
			jbtn_new.setIcon(new ImageIcon(getClass().getResource("/res/New.GIF")));
		}
		return jbtn_new;
	}

	/**
	 * This method initializes jbtn_delete	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtn_delete() {
		if (jbtn_delete == null) {
			jbtn_delete = new JButton();
			jbtn_delete.setBounds(new Rectangle(380, 15, 50, 21));
			jbtn_delete.setToolTipText("DELETE");
			jbtn_delete.setIcon(new ImageIcon(getClass().getResource("/res/Delete.GIF")));
		}
		return jbtn_delete;
	}

	/**
	 * This method initializes jbtn_desfazer	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtn_desfazer() {
		if (jbtn_desfazer == null) {
			jbtn_desfazer = new JButton();
			jbtn_desfazer.setBounds(new Rectangle(435, 15, 50, 21));
			jbtn_desfazer.setToolTipText("UNDO");
			jbtn_desfazer.setIcon(new ImageIcon(getClass().getResource("/res/undo.gif")));
		}
		return jbtn_desfazer;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
